﻿/**
Library: transition managing library (TraM)
Author: Valentyn Derkach 2010.03.25
———————————————————————————————————————————————————————————————————————————————————————————————————————————————————
/**/

package com.bizmlib.tram
{
	import com.bizmlib.utils.sign;
	
	
	public class EngineSet extends Object implements IEngine
	{
		public static const TYPE_MIN:int = 1;
		public static const TYPE_MAX:int = 2;

		public var type:int;
		public var engines:Array;
		private var maxDValue:Number = 0;
		
		
		public function EngineSet(type:int, ... rest)
		{
			this.type = type;
			engines = new Array();
			for(var i:Number=0; i<rest.length; i++)
			{
				var currentEgine:IEngine = rest[i] as IEngne;
				if(!currentEngine)
				{
					throw new Error("Can not instantiate EngineSet: a parameter " + rest[i] + " is not a correct implementation of a IEngine interface.");
				}
				//...
			}
		}
		
		/**
		public function reStart():void
		{
			currentSpeed = initialSpeed;
		}

		public function onBeforeIteration():void
		{
			maxDValue = 0;
			currentSpeed = speedChangeMethod(currentSpeed, initialSpeed, finalSpeed, speedChangeProperties);
		}

		public function performCalculation(propertyName:String, currentValue:Number, endValue:Number):Number
		{
			//trace("performCalculation(" + arguments + ")");
			var newValue:Number = currentValue, d:Number = 0;
			var currentSpeedAbs:Number = Math.abs(currentSpeed);
			
			if(Math.abs(currentValue-endValue)<currentSpeedAbs)
			{
				newValue = endValue;
			}
			else
			{
				d = sign(endValue - currentValue)*currentSpeed;
				newValue = currentValue + d;
			}
			
			maxDValue = Math.max(maxDValue, Math.abs(d));
			
			return newValue;
		}

		public function isFinal():Boolean
		{
			if(maxDValue == 0)
			{
				return true;
			}
			return false;
		}

		public function onAfterIteration():void
		{
		}
		
		
		public static function CHANGE_METHOD_DEFAULT(currentValue:Number, initialValue:Number, finalValue:Number, properties:Object = null):Number
		{
			var engine:IEngine = (properties && properties.engine && (properties.engine is IEngine)) ? (properties.engine as IEngine) : null;
			
			if(engine)
			{
				engine.onBeforeIteration();
				return engine.performCalculation("currentSpeed", currentValue, finalValue);
			}
			
			var step:Number = (properties && !isNaN(properties.step)) ? Number(properties.step) : 1;
			return currentValue + sign(finalValue - initialValue)*Math.min(step, Math.abs(finalValue - currentValue));
		}
		/**/
		
		
		public function toString():String
		{
			return "[EngineSet]";
		}
	}
}